﻿using UnityEngine;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using Holoville.HOTween;

public partial class MessageScene : Scene
{
	public enum TAB_TYPE
	{
		NONE = 0,
		MAIL_MESSAGE,
		BATTLE_MESSAGE,
		REWARD_MESSAGE,
	}

	public enum MSG_BATTLE_CATEGORY
	{
		IV = 1,			// 침략
		CA,				// 반격
		CA_COMP,		// 반격 완료된 메세지
	}

	public enum MSG_REWARD_CATEGORY
	{
		EVENT = 1,		// 이벤트 
		INVESTI,		// 조사 
		EXTEND,			// 확장전 
		FRIEND,			// 친구 선물 
		QUICK_PVP,		// 빠른 PVP
		DISPATCH,		// 파견
		RAID_SUPPRESS,	// 레이드 보스 토벌 보상
		RAID_JOIN,		// 레이드 보스 참여 보상
		RAID_FIND,		// 레이드 보스 발견 보상
		QUEST,			// 퀘스트 완료 보상 
		ACHIEVE,		// 업적 완료 보상 
	}

	public enum MSG_NORMAL_CATEGORY
	{
		NOTICE = 1,
		MAIL,
	}

	public enum SCENE_MODE
	{
		NORMAL,
		WRITE,
	}

	[HideInInspector] public TAB_TYPE currentTab = TAB_TYPE.MAIL_MESSAGE;
	[HideInInspector] public SCENE_MODE currentMode = SCENE_MODE.NORMAL;

	GameObject currentTabObject;

	public GameObject emptyListView;

	public GameObject mailMessagePanel;
	public GameObject battleMessagePanel;
	public GameObject rewardMessagePanel;
	public GameObject writeMessagePanel;

	public UIToggle selectAllMailMessageToggle;
	public UIToggle selectAllBattleMessageToggle;

	public Badge mailBadge;
	public Badge battleBadge;
	public Badge rewardBadge;

	public override void AwakeScene ()
	{
		hasLeftTopMenu = true;
		hasBackButton = true;

		InitGameObject();
	}
	
	public override void StartScene ()
	{
		SelectTab(TAB_TYPE.MAIL_MESSAGE);
	}

	void ShowBadge(BadgeType badgeType)
	{
		NetworkManager.Instance.C2S_ALRAMCOUNT_CLEAR((int)badgeType, (wapperResponseData) => {
			NetworkManager.Instance.C2S_LEGION_GET((responseData) => {
				UserData.Instance.userModel = responseData.data;
				int newFriendBadgeCount = (int)(UserData.Instance.userModel.newAlarmCount & 1023);
				int requestFriendBadgeCount = (int)(UserData.Instance.userModel.newAlarmCount >> 10 & 1023);
				int mailMessageBadgeCount = (int)(UserData.Instance.userModel.newAlarmCount >> 20 & 1023);
				int battleMessageBadgeCount = (int)(UserData.Instance.userModel.newAlarmCount >> 30 & 1023);
				int rewardMessageBadgeCount = (int)(UserData.Instance.userModel.newAlarmCount >> 40 & 1023);

				EPLog.Log ("mailMessageBadgeCount : " + mailMessageBadgeCount);
				EPLog.Log ("battleMessageBadgeCount : " + battleMessageBadgeCount);
				EPLog.Log ("rewardMessageBadgeCount : " + rewardMessageBadgeCount);
				
				if (mailMessageBadgeCount > 0)
				{
					mailBadge.badgeCount = mailMessageBadgeCount;
					NGUITools.SetActive(mailBadge.gameObject, true);
				}
				else
				{
					NGUITools.SetActive(mailBadge.gameObject, false);
				}
				
				if (battleMessageBadgeCount > 0)
				{
					battleBadge.badgeCount = battleMessageBadgeCount;
					NGUITools.SetActive(battleBadge.gameObject, true);
				}
				else
				{
					NGUITools.SetActive(battleBadge.gameObject, false);
				}
				
				if (rewardMessageBadgeCount > 0)
				{
					rewardBadge.badgeCount = rewardMessageBadgeCount;
					NGUITools.SetActive(rewardBadge.gameObject, true);
				}
				else
				{
					NGUITools.SetActive(rewardBadge.gameObject, false);
				}
			});
		});
	}

	public void OnClick_CloseAll()
	{
		PopupUtility.PopAll();
	}

	void InitGameObject()
	{
		NGUITools.SetActive(emptyListView, false);
		selectAllMailMessageToggle.value = false;
		selectAllBattleMessageToggle.value = false;
	}

	public void RestoreCurrentTab()
	{
		SelectTab(currentTab);
	}

	public void SelectTab(TAB_TYPE selectTab)
	{
		if (currentTab == selectTab && currentMode != SCENE_MODE.WRITE)
			return;

		InitGameObject();

		currentTab = selectTab;
		currentMode = SCENE_MODE.NORMAL;

		switch (selectTab)
		{
		case TAB_TYPE.MAIL_MESSAGE:
			NGUITools.SetActive(mailMessagePanel, true);
			NGUITools.SetActive(battleMessagePanel, false);
			NGUITools.SetActive(rewardMessagePanel, false);
			NGUITools.SetActive(writeMessagePanel, false);
			LoadMailMessage();
			break;
			
		case TAB_TYPE.BATTLE_MESSAGE:
			NGUITools.SetActive(mailMessagePanel, false);
			NGUITools.SetActive(battleMessagePanel, true);
			NGUITools.SetActive(rewardMessagePanel, false);
			NGUITools.SetActive(writeMessagePanel, false);
			LoadBattleMessage();
			break;

		case TAB_TYPE.REWARD_MESSAGE:
			NGUITools.SetActive(mailMessagePanel, false);
			NGUITools.SetActive(battleMessagePanel, false);
			NGUITools.SetActive(rewardMessagePanel, true);
			NGUITools.SetActive(writeMessagePanel, false);
			LoadRewardMessage();
			break;
		}
	}

	public void SetEmptyListView(bool setActive)
	{
		NGUITools.SetActive(emptyListView, (setActive ? true : false));
	}

}